package ar.com.ford.it.fullEconomics.logic;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import ar.com.ford.it.fullEconomics.FullEconomicsPrs;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.persistence.dao.MixDao;
import ar.com.ford.it.fullEconomics.persistence.dao.PartDao;
import ar.com.ford.it.fullEconomics.persistence.dao.VehiclelinDao;
import ar.com.ford.it.fullEconomics.persistence.dto.Ncas012VehicleVw;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp01Part;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp06Mix;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;

//****************************************************************
//* Copyright (c) 2004 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   MixLogic.java  $$
//* $$Revision:   1.4  $$
//* $$Author:   TMALDON4  $$
//* $$Date:   Apr 30 2009 16:51:02  $$
//*
//*****************************************************************
public class MixLogic {
    private static final String CLASS_NAME = MixLogic.class.getName();

    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    /**
     * Busca Mix pieza, linea de vehiculo y fecha efectiva
     * 
     * @param code
     * @param name
     * @param plant
     * @return Mix que se correspondan con la b�squeda. Si code y name son
     * vac�os, entonces busca todos los Mix
     * @throws FullEconomicsException
     */
    public Collection<Teesp06Mix> findMix(Teesp01Part part,
                                          Ncas012VehicleVw veiculoS,
                                          Timestamp effectiveDate, String month)
            throws FullEconomicsException {

        String METHOD_NAME = "findMix";
        log.entering(CLASS_NAME, METHOD_NAME);
        MixDao dao = new MixDao();
        Teesp06Mix mix = new Teesp06Mix();
        Collection<Teesp06Mix> ret = null;
        Collection<Teesp06Mix> retFilter = null;
        mix.setTeesp01Part(part);
        mix.setEesp06VehicleCodeC(veiculoS);
        mix.setEesp06MixEffectiveDateY(effectiveDate);
        mix.setEesp06PlantCodeC(part.getEesp01PlantCodeC());
        try {
            ret = dao.findMix(mix);
            retFilter = ret;
            if (effectiveDate == null && !month.equals("-1")) {
                retFilter = new ArrayList<Teesp06Mix>();
                for (Teesp06Mix mixR : ret) {
                    if (mixR.getEesp06MixEffectiveDateY().toString().substring(
                            5, 7).equals(month))
                        retFilter.add(mixR);
                }
            }
        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "findMix", e);
            throw new FullEconomicsException(e,
                    FullEconomicsPrs.MessageErrors.Mix.FIND);
        }
        log.exiting(CLASS_NAME, METHOD_NAME);
        return retFilter;
    }

    /**
     * Crear Mix
     * 
     * @param mix
     * @param user
     * @throws FullEconomicsException
     */
    public void createMix(Teesp06Mix mix, FullEconomicsWslxUser user)
            throws FullEconomicsException {
        String METHOD_NAME = "createMix";
        log.entering(CLASS_NAME, METHOD_NAME);
        Teesp06Mix mixN = new Teesp06Mix();
        MixDao dao = new MixDao();
        PartDao daoPart = new PartDao();
        VehiclelinDao daoMix = new VehiclelinDao();
        try {
            PersistenceUtil.newTransaction();
            mixN.setEesp06CreateUserC(user.getId());
            mixN.setEesp06LastUpdtUserC(user.getId());
            mixN.setEesp06PlantCodeC(user.getTeesg01Plant());
            mixN.setEesp06CreateS(new Timestamp(System.currentTimeMillis()));
            mixN.setEesp06LastUpdtS(new Timestamp(System.currentTimeMillis()));
            mixN.setEesp06VehicleCodeC(daoMix.getVehiclelinUpdateByPk(mix
                    .getEesp06VehicleCodeC()));
            mixN.setEesp06MixEffectiveDateY(mix.getEesp06MixEffectiveDateY());
            mixN.setEesp06MixUsageQuantityQ(mix.getEesp06MixUsageQuantityQ());
            mixN.setEesp06MixValuePercentP(mix.getEesp06MixValuePercentP().divide(new BigDecimal(100)));
            mixN.setTeesp01Part(daoPart.getPartForUpdate(mix.getTeesp01Part()
                    .getEesp01PartPrefixCodeC(), mix.getTeesp01Part()
                    .getEesp01PartBaseCodeC(), mix.getTeesp01Part()
                    .getEesp01PartSuffixCodeC(), mix.getTeesp01Part()
                    .getEesp01PartClassCodeC(), user.getTeesg01Plant()));
            dao.createMix(mixN);
            PersistenceUtil.commitTransaction();
        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "createMix", e);
            throw new FullEconomicsException(e,
                    FullEconomicsPrs.MessageErrors.Mix.CREATE);
        }
        log.exiting(CLASS_NAME, METHOD_NAME);

    }

    /**
     * Actualiza el mix solo los porcentajes de mix mas el per unit
     * 
     * @param mix
     * @param user
     * @throws FullEconomicsException
     */
    public void updateMix(Teesp06Mix mix, FullEconomicsWslxUser user)
            throws FullEconomicsException {

        String METHOD_NAME = "updateMix";
        log.entering(CLASS_NAME, METHOD_NAME);
        Teesp06Mix workingCopy = new Teesp06Mix();
        MixDao dao = new MixDao();
        try {
            PersistenceUtil.newTransaction();
            PersistenceUtil.getPersistenceManager().clearCache();
            workingCopy = dao.getUpdateMix(mix);
            workingCopy.setEesp06LastUpdtUserC(user.getId());
            workingCopy.setEesp06LastUpdtS(new Timestamp(System
                    .currentTimeMillis()));
            workingCopy.setEesp06MixUsageQuantityQ(mix
                    .getEesp06MixUsageQuantityQ());
            workingCopy.setEesp06MixValuePercentP(mix
                    .getEesp06MixValuePercentP());
            PersistenceUtil.commitTransaction();
        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "update Mix", e);
            throw new FullEconomicsException(e,
                    FullEconomicsPrs.MessageErrors.Mix.UPDATE);
        }
        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Elimina un mix
     * 
     * @param mix
     * @throws FullEconomicsException
     */
    public void deleteMix(Teesp06Mix mix) throws FullEconomicsException {
        String METHOD_NAME = "deleteMix";
        log.entering(CLASS_NAME, METHOD_NAME);
        MixDao dao = new MixDao();
        try {
            PersistenceUtil.newTransaction();
            dao.deleteMix(mix);
            PersistenceUtil.commitTransaction();
        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "deleteMix", e);
            throw new FullEconomicsException(e,
                    FullEconomicsPrs.MessageErrors.Mix.DELETE);
        }
        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Pregunta si es un unico resultado
     * 
     * @param name
     * @param plant
     * @return boolean
     * @throws FullEconomicsException
     */
    public Teesp06Mix getMixById(Teesp06Mix mix) throws FullEconomicsException {
        String METHOD_NAME = "mixNameAlreadyExists";
        log.entering(CLASS_NAME, METHOD_NAME);
        MixDao dao = new MixDao();
        Teesp06Mix ret;

        try {
            ret = dao.getMixbyId(mix);
        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "mixNameAlreadyExists", e);
            throw new FullEconomicsException(e,
                    FullEconomicsPrs.MessageErrors.Mix.GETBYID);
        }
        log.exiting(CLASS_NAME, METHOD_NAME);
        return ret;
    }



}
